Completed
Pull Request — master (#67)
by
unknown
02:14
created

Page.js ➔ ... ➔ ???   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
nop 1
1
import Handlebars from 'handlebars'
2
import path from 'path'
3
import fse from 'fs-extra'
4
5
import {
6
  abeEngine,
7
  cmsData,
8
  cmsTemplates,
9
  config,
10
  abeExtend,
11
  Manager
12
} from '../'
13
14
/**
15
 * Page class
16
 * manage HTML generation for page template
17
 */
18
export default class Page {
19
20
  /**
21
   * Create new page object
22
   * @param  {Object} params req.params from express route
0 ignored issues
show
Documentation introduced by
The parameter params does not exist. Did you maybe forget to remove this comment?
Loading history...
23
   * @param  {Object} i18n translation
0 ignored issues
show
Documentation introduced by
The parameter i18n does not exist. Did you maybe forget to remove this comment?
Loading history...
24
   * @param  {Function} callback 
0 ignored issues
show
Documentation introduced by
The parameter callback does not exist. Did you maybe forget to remove this comment?
Loading history...
25
   * @param  {Boolean} onlyHTML default = false, if true HTML content will contains abe attributes
26
   * @return {String} HTML page as string
27
   */
28
  constructor(templateId, template, json, onlyHTML = false) {
29
    // HOOKS beforePageJson
30
    json = abeExtend.hooks.instance.trigger('beforePageJson', json)
31
32
    abeEngine.instance.content = json
33
      
34
    if(typeof Handlebars.templates[templateId] !== 'undefined' && 
35
        Handlebars.templates[templateId] !== null && 
36
        config.files.templates.precompile
37
      ){
38
39
      template = Handlebars.templates[templateId]
40
      this.html = template(json, {data: {intl: config.intlData}})
41
42
      //console.log('precompile')
43
44
    } else {
45
46
      this._onlyHTML = onlyHTML
47
      this.template = template
48
      this.HbsTemplatePath = path.join(config.root, config.templates.url, 'hbs/'+templateId+'.hbs')
49
50
      // This pattern finds all abe tags which are not enclosed in a html tag attribute
51
      // it finds this one: <title>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</title>
52
      // it excludes this one: <meta name="description" content='{{abe type="text" key="meta_description" desc="Meta description" tab="Meta" order="4100"}}"/> 
53
      this.abePattern = /[^"']({{abe.*?type=[\'|\"][text|rich|textarea]+[\'|\"][\s\S].*?}})/g
54
55
      // This pattern finds all abe tags enclosed in a HTML tag attribute
56
      this.abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1}{{abe.*?}})/g
57
58
      // This pattern finds all {{#each ...}}...{{/each}} blocks
59
      this.eachBlockPattern = />\s*(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g
60
61
      // This pattern finds all {{#each ...}}...{{/each}} blocks
62
      this.blockPattern = /(\{\{#each.*\}\}[\s\S]*?\{\{\/each\}\})/g
63
64
      // Remove text with attribute "visible=false"
65
      this.template = cmsTemplates.prepare.removeHiddenAbeTag(this.template)
66
    
67
      if(!this._onlyHTML) {
68
69
        // Surrounds each Abe tag (which are text/rich/textarea and not in html attribute) with <abe> tag
70
        // ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title>
71
        this.template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template)
72
      }
73
      else {
74
        this.template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template)
75
        this.template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template)
76
      }
77
78
      // je rajoute les index pour chaque bloc lié à un each
79
      this.template = cmsTemplates.prepare.indexEachBlocks(this.template, this._onlyHTML)
80
      
81
      if(!this._onlyHTML){
82
83
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.template)
84
85
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.template)
86
87
        this.template = cmsTemplates.prepare.addAbeSourceComment(this.template, json)
88
      }
89
90
      // We remove the {{abe type=data ...}} from the text 
91
      this.template = cmsData.source.removeDataList(this.template)
92
93
      // It's time to replace the [index] by {{@index}} (concerning each blocks)
94
      this.template = cmsTemplates.prepare.replaceAbeEachIndex(this.template)
95
96
      if(config.files.templates.precompile){
97
        // Let's persist the precompiled template for future use (kind of cache)
98
        fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
99
        Manager.instance.addHbsTemplate(templateId)
100
      }
101
102
      // I compile the text
103
      var compiledTemplate = Handlebars.compile(cmsTemplates.insertDebugtoolUtilities(this.template, this._onlyHTML))
104
105
      // I create the html page ! yeah !!!
106
      this.html = compiledTemplate(json, {data: {intl: config.intlData}})
107
    }
108
109
    if(this._onlyHTML) {
110
      this.html = abeExtend.hooks.instance.trigger('afterPageSaveCompile', this.html, json)
111
    }else {
112
      this.html = abeExtend.hooks.instance.trigger('afterPageEditorCompile', this.html, json)
113
    }
114
  }
115
}